package com.es.hr.baoxiaoShenpi;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.es.common.CommonBean;
import com.es.common.CommonBiz;
import com.es.hr.baoxiao.BaoxiaoBean;
import com.es.hr.baoxiao.BaoxiaoBiz;
import com.es.main.LoginUserBeen;
import com.es.util.page.PageBean;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;

public class SearchBaoxiaoShenpiInitAction extends ActionSupport
implements ServletRequestAware{
	 HttpServletRequest request;

	public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
		// TODO Auto-generated method stub
		this.request = paramHttpServletRequest;
	}
	
	public String execute() throws Exception{
		String result = "";
		String message = "";
		BaoxiaoShenpiBiz baoxiaoShenpiBiz = new BaoxiaoShenpiBiz();
		BaoxiaoBiz baoxiaoBiz = new BaoxiaoBiz();
		
		LoginUserBeen userLogin = (LoginUserBeen)request.getSession().getAttribute("loginuser");
		String role = userLogin.getLoginRole();
		
		Date date = new Date();
		int year = date.getYear() + 1900;
		int mouth = date.getMonth() + 1;
		int day = date.getDate();
		
		String syear = new Integer(year).toString();
		String smouth = new Integer(mouth).toString();
		if(smouth.length() < 2) smouth = "0" + smouth;
		String sday = new Integer(day).toString();
		if(sday.length() < 2) sday = "0" + sday;
		
		String baoxiao_date = syear + "-" + smouth + "-" + sday;
		//限制5号以内才能报销审批
		com.es.common.CommonBean riqibean = new CommonBiz().getCommonByTypeAndCode("baoxiaoproperty", "baoxiaoshenpiriqi");
		int riqi = riqibean == null?3:new Integer(riqibean.getCommonValue()).intValue();
		if(day > riqi){
			message = "每个月前"+riqi+"天才能审批";
			this.request.setAttribute("message", message);
			return "error";
		}
		BaoxiaoBean bean = baoxiaoBiz.getBaoxiaoBaseInfoFromEmp(userLogin.getLoginId());
		
		CommonBean rtmrole = new CommonBiz().getCommonByTypeAndCode("role", "RTM");
		CommonBean shiyebu = new CommonBiz().getCommonByTypeAndCode("shiyebu", "ITMall");
		CommonBean shiyeburole = new CommonBiz().getCommonByTypeAndCode("role", "shiyebu");
		
		this.request.setAttribute("areaList", new com.es.util.common.CommonBiz(userLogin).getAreaList());
		
		if(role.equals(rtmrole.getCommonValue()) && userLogin.getShiyebu().equals(shiyebu.getCommonValue())){
			
			PageBean res = baoxiaoShenpiBiz.searchBaoxiaoShenpi("","", "", userLogin.getLoginId(), bean.getCtm_name(),"","","0","", 1,true);
			this.request.setAttribute("pageBean", res);
			this.request.setAttribute("isZongbu", "");
			this.request.setAttribute("rtm_code", bean.getCtm_code());
			this.request.setAttribute("rtm_name", bean.getCtm_name());
			result = "success";
			return result;
		}	//总部，系统管理员，itmall的事业部管理员可审批
		else if(role.equals("1") || role.equals("zongbu") || (role.equals(shiyeburole.getCommonValue())) && userLogin.getShiyebu().equals(shiyebu.getCommonValue())){
			PageBean res = baoxiaoShenpiBiz.searchBaoxiaoShenpi("","", "", "", "","","","","0", 1,false);
			this.request.setAttribute("pageBean", res);
			this.request.setAttribute("isZongbu", "true");
			result = "success";
			return result;
		}else{
			 message = "您没有权限进行报销审批！";
			this.request.setAttribute("message", message);
			return "error";
		}
	}

}
